//
//  PresentationController.m
//  CKApp
//
//  Created by CC on 2020/9/4.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "PresentationController.h"

@interface PresentationController ()
@end

@implementation PresentationController

- (void)presentationTransitionWillBegin {
  [super presentationTransitionWillBegin];
  if (self.containerView != nil) {
    self.backgroundView = [[UIView alloc] initWithFrame:self.containerView.bounds];
    [self.containerView insertSubview:self.backgroundView atIndex:0];
    self.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self excuteBackgroundAnimation];
  }
}

- (void)dismissalTransitionWillBegin {
  [super dismissalTransitionWillBegin];
  [self excuteBackgroundDismissAnimation];
}

- (BOOL)shouldRemovePresentersView {
  return NO;
}

- (void)excuteBackgroundAnimation {
  self.backgroundView.alpha = 0.0;
  if (self.presentedViewController.transitionCoordinator != nil) {
    [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
      self.backgroundView.alpha = 1.0;
    } completion:nil];
  }
}

- (void)excuteBackgroundDismissAnimation {
  if (self.presentedViewController.transitionCoordinator != nil) {
    [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
      self.backgroundView.alpha = 0.0;
    } completion:nil];
  }
}

@end
